package cn.sh.tools.util.file;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import lombok.extern.slf4j.Slf4j;

/**
 * @author shihao
 * @description 加密工具
 */
@Slf4j
public class SecureUtils {

    public static String getEncryptPwdByDes(String miPwd, String key) {
        byte[] secretKey = SecureUtil.generateKey(SymmetricAlgorithm.DES.getValue(),
                key.getBytes()).getEncoded();
        return SecureUtil.des(secretKey).encryptHex(miPwd);
    }

    public static String getDecryptPwdByDes(String anPwd, String key) {
        byte[] secretKey = SecureUtil.generateKey(SymmetricAlgorithm.DES.getValue(),
                key.getBytes()).getEncoded();

        return SecureUtil.des(secretKey).decryptStr(anPwd);
    }

    public static void generatorByDes(String miPwd, String key) {
        log.info("source password: {}", miPwd);

        // 密钥
        byte[] secretKey = SecureUtil.generateKey(SymmetricAlgorithm.DES.getValue(),
                key.getBytes()).getEncoded();
        // 1- 加密
        String anPwd = SecureUtil.des(secretKey).encryptHex(miPwd);
        log.info("encrypt password: {}", anPwd);
        // 2- 解密
        String decryptPassword = SecureUtil.des(secretKey).decryptStr(anPwd);
        log.info("decrypt password: {}", decryptPassword);
    }

}
